package org.song.service;

import org.song.model.dto.UserDto;
import org.song.model.entity.UserBlogOperateEntity;
import org.song.model.threadlocal.WebContextUtil;
import org.song.model.vo.User;
import org.song.util.FunctionUtil;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

public interface UserService {
    List<User> listUsers( @NonNull UserDto userDto);

    Integer deleteUser( @NonNull User user);

    Integer insertUser( @NonNull User user);

    Integer updateUser( @NonNull User user);

    /**
     * 用户博客操作接口
     * @param userBlogOperateEntity
     * @return
     */
    @NonNull
    Integer insertUserBlogOperate( @NonNull UserBlogOperateEntity userBlogOperateEntity);

    /**
     * 判断当前用户是否操作过该博客
     * @param userBlogOperateEntity
     * @return
     */
    @NonNull
    Integer getUserBlogOperate( @NonNull UserBlogOperateEntity userBlogOperateEntity );

    @Nullable
    Integer deleteUserBlogOperate( @NonNull UserBlogOperateEntity userBlogOperateEntity);

    /**
     * 判断当前用户是否登录，userId为空说明用户未登录对未登录的用户使用IP地址作为用户Id
     * @param userBlogOperateEntity
     * @return
     */
    default void updateUserLoginInfo( @NonNull UserBlogOperateEntity userBlogOperateEntity ){
        HttpServletRequest currentRequest = WebContextUtil.getCurrentRequest();
        // 用户Id判断 为空则说明未更新 从ThreadLocal中取出请求IP作为用户Id
        userBlogOperateEntity.setUserId( Optional.ofNullable( userBlogOperateEntity.getUserId() )
                .orElseGet( () -> FunctionUtil.getIpAddress( WebContextUtil.getCurrentRequest()) ) );
    }

    /**
     * 判断用户是否是首次访问
     * @return
     */
    @Nullable
    Boolean firstVisit();

}
